package com.molihub.service.impl;

import com.molihub.service.FtpService;
import com.molihub.utils.FtpUtil;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

/**
 * @ClassName: FtpServiceImpl
 * @Description: TODO
 * @author: xw
 * @date 2020/2/6 16:00
 * @Version: 1.0
 **/
@Service
public class FtpServiceImpl extends BaseService implements FtpService {
    @Override
    public boolean uploadFile(String path, String fileName, InputStream in) {
        return FtpUtil.uploadFile(path,fileName,in);
    }

    @Override
    public boolean downloadFile(String path, String fileName,String localPath) {
        return FtpUtil.downloadFile(path,fileName,localPath);
    }

    @Override
    public List<List<String>> getAllFilesNameByPath(String path) {
        return FtpUtil.getAllFilesNameByPath(path);
    }

    @Override
    public boolean deleteFile(String path, String fileName) {
        return FtpUtil.deleteFile(path,fileName);
    }

    @Override
    public boolean deleteFolder(String path) {
        return FtpUtil.deleteFolder(path);
    }

    @Override
    public boolean deleteFolderForce(String path) {
        return deleteFolderF(path);
    }

    @Override
    public boolean updateFileName(String oldPath, String newPath) {
        return FtpUtil.reNameFile(oldPath,newPath);
    }

    public boolean deleteFolderF(String path){
        List<List<String>> result = getAllFilesNameByPath(path);
        List<String> files = result.get(0);
        List<String> folders = result.get(1);
        System.out.println("当前目录："+path);
        System.out.println("当前目录下所有文件："+files);
        System.out.println("当前目录下所有文件夹："+folders);

        //当前为空文件夹
        if (files.size()==0 && folders.size()==0){
            deleteFolder(path);
            return true;
        }else{
            //非空文件夹

            //删除当前目录下所有文件
            if (files.size()!= 0){
                for (int i = 0; i < files.size(); i++) {
                    System.out.println("删除的文件"+path+files.get(i));
                    deleteFile(path,files.get(i));
                }
            }
            //删除当前目录下所有子文件夹
            if (folders.size()!= 0){
                for (int i = 0; i < folders.size(); i++) {
                    System.out.println("删除文件夹"+folders.get(i));
                    deleteFolderF(path+"/"+folders.get(i));
                    deleteFolder(path+"/"+folders.get(i));
                }
            }
            return true;
        }
    }
}
